package com.go.where.manager.action.go;

import java.io.File;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;

import com.go.where.manager.action.SuperAction;
import com.go.where.manager.common.Constants;
import com.go.where.manager.page.Pagination;
import com.go.where.manager.pojo.TeacherInfo;
import com.go.where.manager.service.TeacherService;
import com.go.where.manager.util.CommonUtil;
import com.go.where.manager.util.KindEditUtil;

@InterceptorRefs( {
		@InterceptorRef(value = "defaultStack", params = { "maximumSize",
				"2048000", "allowedTypes",
				"image/bmp,image/png,image/gif,image/jpeg" }),
		@InterceptorRef(value = "defalutStackSet") })
public class TeacherAction extends SuperAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4383195694100314090L;
	private static final Logger log = Logger.getLogger(TeacherAction.class);

	public Pagination<?> page;

	@Resource(name = "teacherService")
	private TeacherService teacherService;

	private TeacherInfo teacherInfo;
	private TeacherInfo teacherInfoQuery;

	private String teacherId;

	private File header;// 上传的文件
	private String headerFileName; // 文件名称
	private String headerContentType; // 文件类型

	/**
	 * 查询列表
	 * 
	 * @return
	 */
	public String list() {
		if (teacherInfoQuery == null) {
			teacherInfoQuery = new TeacherInfo();
			teacherInfoQuery.setPageSize(Constants.INIT_PAGESIZE);
			teacherInfoQuery.setCurrentPage(Constants.INIT_CURRENTPAGE);
		}
		try {
			teacherInfoQuery.setIsDelete(Constants.NOT_DELETED);
			page = teacherService.queryPage(teacherInfoQuery);
		} catch (Exception e) {
			addActionError(e.getMessage());
			log.error("查询信息异常", e);
		}
		return LIST;
	}

	public String toEdit() {
		teacherInfo = teacherService.queryByPrimartKey(teacherId);
		return INPUT;
	}

	public String add() {
		return INPUT;
	}

	/**
	 * 保存
	 * 
	 * @return
	 */
	public String save() {
		try {
			String headerRealpath = ServletActionContext.getServletContext()
					.getRealPath(
							CommonUtil.FILE_SEPARATOR
									+ Constants.TEACHER_HEADER_IMAGES);
			String headerPath = KindEditUtil.uploadPicture(header,
					headerFileName, headerRealpath,
					Constants.TEACHER_HEADER_IMAGES);
			teacherInfo.setHeaderPath(headerPath);
			teacherService.save(teacherInfo, getCurrentOperator());
			redirectionUrl = "teacher!list.htm";
			return SUCCESS;
		} catch (Exception e) {
			log.error("保存信息异常", e);
			addActionError("保存信息异常！" + e);
			return this.add();
		}
	}

	/**
	 * 编辑
	 * 
	 * @return
	 */
	public String edit() {
		try {
			if (header != null) {
				String headerRealpath = ServletActionContext
						.getServletContext().getRealPath(
								CommonUtil.FILE_SEPARATOR
										+ Constants.TEACHER_HEADER_IMAGES);
				String headerPath = KindEditUtil.uploadPicture(header,
						headerFileName, headerRealpath,
						Constants.TEACHER_HEADER_IMAGES);
				teacherInfo.setHeaderPath(headerPath);
			}
			teacherService.update(teacherInfo, getCurrentOperator());
			redirectionUrl = "teacher!list.htm";
			return SUCCESS;
		} catch (Exception e) {
			log.error("修改信息异常", e);
			addActionError("修改信息异常！" + e);
			teacherId = teacherInfo.getTeacherId();
			return this.toEdit();
		}
	}

	/**
	 * 详情
	 * 
	 * @return
	 */
	public String detail() {
		teacherInfo = teacherService.queryByPrimartKey(teacherId);
		return "detail";
	}

	/**
	 * 假删除
	 * 
	 * @return
	 */
	public String falseDelete() {
		teacherInfo = new TeacherInfo();
		teacherInfo.setTeacherId(teacherId);
		teacherInfo.setIsDelete(Constants.DELETED);
		try {
			teacherService.update(teacherInfo, getCurrentOperator());
			redirectionUrl = "teacher!list.htm";
			return SUCCESS;
		} catch (Exception e) {
			log.error("删除信息异常", e);
			addActionError("删除信息异常" + e);
			return this.list();
		}
	}

	public Pagination<?> getPage() {
		return page;
	}

	public void setPage(Pagination<?> page) {
		this.page = page;
	}

	public TeacherInfo getTeacherInfo() {
		return teacherInfo;
	}

	public void setTeacherInfo(TeacherInfo teacherInfo) {
		this.teacherInfo = teacherInfo;
	}

	public TeacherInfo getTeacherInfoQuery() {
		return teacherInfoQuery;
	}

	public void setTeacherInfoQuery(TeacherInfo teacherInfoQuery) {
		this.teacherInfoQuery = teacherInfoQuery;
	}

	public String getTeacherId() {
		return teacherId;
	}

	public void setTeacherId(String teacherId) {
		this.teacherId = teacherId;
	}

	public File getHeader() {
		return header;
	}

	public void setHeader(File header) {
		this.header = header;
	}

	public String getHeaderFileName() {
		return headerFileName;
	}

	public void setHeaderFileName(String headerFileName) {
		this.headerFileName = headerFileName;
	}

	public String getHeaderContentType() {
		return headerContentType;
	}

	public void setHeaderContentType(String headerContentType) {
		this.headerContentType = headerContentType;
	}

}
